package com.educate.schedule;

import com.educate.common.lang.ProblemRedisNameConst;
import com.educate.service.ProblemService;
import com.educate.service.impl.ProblemServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

/**
 * @author xiongyu
 * @date 2021/12/16 18:30
 */
@Component
public class RedisScheduler {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ProblemService problemService;

    /**
     * 每隔12小时将redis中点赞数存在数据库中
     */
    @Scheduled(fixedRate = 12 * 60 * 60 * 1000)
    public void flushDatabaseProblemThumbsUp(){
        Map<String, Integer> map = redisTemplate.opsForHash().entries(ProblemRedisNameConst.PROBLEMLIKENUMBER);
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
           problemService.updateProblemThumbsUp(Long.parseLong(entry.getKey()),entry.getValue());
        }

    }

    /**
     * 每隔10分钟刷新热点问题
     */
    @Scheduled(fixedRate = 10 * 60 * 1000)
    public void flushHotProblemThumbsUp(){
        ProblemServiceImpl.hotProblemIds.clear();
    }

}



















